<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--[if lt IE 9]><meta http-equiv="X-UA-Compatible" content="chrome=1" /><![endif]-->
    <!--[if IE 9]><meta http-equiv="X-UA-Compatible" content="IE9" /><![endif]-->
    <meta charset="UTF-8" />
    
    <title>jOWE ~ Examples ~ jowe-ui-2d-hexa.js</title>
    
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="jowe-core.js"></script>
    <script type="text/javascript" src="jowe-alea.js"></script>
    <script type="text/javascript" src="jowe-ui-2d-hexa.js"></script>

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20897630-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

</head>
<!--
********************************************************************************
jOWE - javascript Opensource Word Engine
http://code.google.com/p/jowe/
********************************************************************************

Copyright (c) 2010-2012 Ludovic L.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

********************************************************************************

How to display a map with hexagonal tiles from an HeightMap.

-->
<body>
<em>This page is a part of the <a href="http://code.google.com/p/jowe/">jOWE</a> project - a javascript Opensource Word Engine</em>
<hr />
  <p>
    We've seen in the <a href="examples-jowe-core-colored.html">previous part</a> how to draw a simple colored map in HTML.<br />
    This page explains how to have a real graphical representation of your previously created HeightMap object.
  </p>
  
  <h1>Initialization:</h1>

  <p>First of all, we declare and create our heightmap.</p>

<pre>  <font color="green">// Create an object with default values :
  // pitch = 8, ratio = 3.1, width = 127, height = 127</font>
  <i><font color="blue">var</font></i> oHeightMap = <i><font color="blue">new</font></i> HeightMap();
  <font color="green">// Generate a map (direct calls to "setSide", "makeMap", "smooth" and "crop").</font>
  oHeightMap.doMap(10,12);
</pre>

  <h1>Drawing a map with hexagonal tiles:</h1>

  <p>If you want to have a map representation with hexagonal tiles you can use <b>jowe_ui_2d_hexa</b> object.
  You need to place a CANVAS tag in your HTML code, it will be used to paint the map.
  Don't forget to give it a unique id, other attributes are optionals.</p>
<pre>  <font color="blue">&lt;canvas</font> <font color="red">id</font>=<font color="purple">"cmap"</font> <font color="red">width</font>=<font color="purple">"1"</font> <font color="red">height</font>=<font color="purple">"1"</font><font color="blue">&gt;</font>
  <font color="blue">&lt;/canvas&gt;</font>
</pre>  

  <p>Creating the UI object requires 4 parameters. You have to specify the id of the canvas to work on, and 
  the width and height to give to the canvas object. The background color is optional (default is black).<br />
  Once your object is initialized, you only have to call the draw method with the required parameters :
    <ul>
      <li>the heightmap array</li>
      <li>the rotation angle used to display the map</li>
      <li>x and y offset from where display the map, the (0,0) coordinate correspond to the middle of the map.</li>
      <li>width and height of an hexagonal tile</li>
      <li>array of colors to paint the tiles depending on their height</li>
    </ul>
  </p>

  <pre>  <font color="green">// Initialize the colors used to paint the map (0 to 8), according to the pitch used to generate the heightmap.</font>
  <i><font color="blue">var</font></i> colors = ['#0000ee', '#0000ee', '#0033dd', '#20F220', '#10D210', '#00B200', '#505050', '#505050', '#DADADA'];
  
  <font color="green">// Initialize the UI object and set the size of the canvas to 320x160 pixels.</font>
  <i><font color="blue">var</font></i> oCanvasMap = <i><font color="blue">new</font></i> jowe_ui_2d_hexa("cmap", 320, 160, '#000');
  <font color="green">// Draw the map without rotation, from offset (0,0) and tile size of 16x16.</font>
  oCanvasMap.draw(oHeightMap.item, 0, 0, 0, 16, 16, colors);
  </pre>

  <canvas id="tmap" width="1" height="1" style="margin-left:40px;">
  </canvas>
  
  <script type="text/javascript">
    var colors = ['#0000ee','#0000ee','#0033dd','#20F220','#10D210','#00B200','#505050','#505050','#DADADA'],
        oHMap  = new HeightMap(),
        oCMap  = new jowe_ui_2d_hexa("tmap", 320, 160, '#000');
    oHMap.setAleaSeed('jowe');
    oHMap.doMap(10, 12);
    oCMap.draw(oHMap.item, 0, 0, 0, 16, 16, colors);
  </script>

  <p>If you'd rather have a straight map, you can use an optional parameter :</p>
  <pre>  <font color="green">// Draw a straight map without rotation, from offset (0,0) and tile size of 16x16.</font>
  oCanvasMap.draw(oHeightMap.item, 0, 0, 0, 16, 16, colors, <font color="blue"><b>true</b></font>);
  </pre>

  <canvas id="smap" width="1" height="1" style="margin-left:40px;">
  </canvas>
  
  <script type="text/javascript">
    oCMap = new jowe_ui_2d_hexa("smap", 320, 160, '#000');
    oCMap.draw(oHMap.item, 0, 0, 0, 16, 16, colors, true);
  </script>
  
  <h1>Try it :</h1>
  
  <p>This simple demo let you interact with all parameters in a very minimalistic way.
  Its sole purpose is to permit you to test the function.</p>
  
  <canvas id="cmap" width="1" height="1">
  </canvas>
  
  <div>
    <div>
      Enter 'width' x 'height' and generate your heightmap.<br />
      <input id="twidth" type="text" value="65" size="6" maxlength="3" style="text-align:right;">
        x
      <input id="theight" type="text" value="45" size="6" maxlength="3" style="text-align:right;">
      <input id="tsubmit" type="submit" value="Generate"><br /><br />
      Manipulate and navigate the generated map with the buttons below.
    </div>

    <div style="width:95px;float:left;text-align:center;">
      <div id="nfoAngle"></div>
      <button id="ap">A+5</button><button id="am">A-5</button>
    </div>
    <div style="width:95px;float:left;text-align:center;">
      <div id="nfoX"></div>
      <button id="xp">X+5</button><button id="xm">X-5</button>
    </div>
    <div style="width:95px;float:left;text-align:center;">
      <div id="nfoY"></div>
      <button id="yp">Y+5</button><button id="ym">Y-5</button>
    </div>
    <div style="width:95px;float:left;text-align:center;">
      <div id="nfoW"></div>
      <button id="wp" class="inc1">W+1</button><button id="wm" class="inc1">W-1</button>
    </div>
    <div style="width:95px;float:left;text-align:center;">
      <div id="nfoH"></div>
      <button id="hp" class="inc1">H+1</button><button id="hm" class="inc1">H-1</button>
    </div>
    <div style="width:115px;float:left;text-align:center;">
      <div id="nfoD"></div>
      <button id="dp" class="inc0">D+0.1</button><button id="dm" class="inc0">D-0.1</button>
    </div>
    <div style="width:100px;float:left;">
      <br />
      <input id="mapType" type="checkbox" value="alt" checked="checked">Straight map
    </div>
  </div>

  <div id="tresult" style="clear:both;">
    
  </div>

<script type="text/javascript">

  var height_color = ['#0000ee','#0000ee','#0033dd','#20F220','#10D210','#00B200','#505050','#505050','#DADADA'],
      oHeightMap,
      oCanvasMap,
      param = {a : 0, x : 0, y : 0, h : 10, w : 9, s : true, d : 0.5};

function display_values()
{
    $("#nfoAngle").html('Angle:' + param.a);
    $("#nfoX").html('X:' + param.x);
    $("#nfoY").html('Y:' + param.y);
    $("#nfoH").html('H:' + param.h);
    $("#nfoW").html('W:' + param.w);
    $("#nfoD").html('D:' + param.d);
    $("#mapType").attr("checked", param.s);
}

function CanvasDraw() {
  oCanvasMap.draw(oHeightMap.item, param.a, param.x, param.y, param.h, param.w, height_color, param.s, param.d);
}

function run_test(w, h)
{
  var text_dbg;
  // Create object with default values :
  // pitch = 8, ratio = 3.1, width = 127, height = 127
  oHeightMap = new HeightMap();
  // Create UI object.
  oCanvasMap = new jowe_ui_2d_hexa("cmap", 580, 300, '#000');
  
  // Start debug time calculation.
  var start = new Date().getTime();
  
  // Shortcut to generate a map (direct calls to "setSide", "makeMap", "smooth" and "crop").
  oHeightMap.doMap(w, h);
  
  // End debug time calculation.
  var end = new Date().getTime();
  
  // Display processing time.
  text_dbg = "<p><em>Heightmap processed in " + (end - start) + "ms</em><br />";

  // Start debug time calculation.
  var start = new Date().getTime();
                     
  CanvasDraw();

  // End debug time calculation.
  var end = new Date().getTime();
  
  // Display processing time.
  $("#tresult").html(text_dbg + "<em>Hexagonal map drawn in " + (end - start) + "ms</em></p>");

}

$(function () {

  // First loading, display values.
  display_values();

  // Generate and draw map on submit.
  $("#tsubmit").click(function() {
    run_test( 1 * $("#twidth").val(), 1 * $("#theight").val() );
  });

  $("#ap, #am, #xp, #xm, #yp, #ym, #hp, #hm, #wp, #wm, #dp, #dm, #mapType").click(function(){
  
    var inc = (this.className == "inc1" ? 1 : (this.className == "inc0" ? 0.1 : 5));
    // According to the id, increase or decrease value of the given parameter.
    if (this.id.substr(1,1) == 'p') {
      param[this.id.substr(0,1)] += inc;
    } else if (this.id.substr(1,1) == 'm') {
      param[this.id.substr(0,1)] -= inc;
    }
    
    // Adjust values if it goes to far.
    if (param.a >= 360) param.a = 0;
    if (param.a < 0) param.a = 355;
    if (param.w < 4) param.w = 4;
    if (param.h < 4) param.h = 4;
    if (param.d < 0) param.d = 0;
    param.d = 1 * param.d.toFixed(1);
    param.s = $("#mapType").is(':checked');
    // Display values...
    display_values();
    // ...and draw the canvas.
    CanvasDraw();
  });
});
  
</script>

  <h2>What's next?</h2>

  <p>
    You also may be interested by drawing map with <a href="examples-jowe-ui-2d-diam.html">diamond tiles</a>.<br />
    You also may be interested by drawing a <a href="examples-jowe-ui-2d-pixel.html">pixel map</a>.
  </p>

<hr />
<em>This page is a part of the <a href="http://code.google.com/p/jowe/">jOWE</a> project - a javascript Opensource Word Engine</em>
</body>
</html>